package com.etoak.common.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class RedisService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     *String类型 set
     */
    public void setex(String key, String value, long timeout, TimeUnit timeUnit) {
        stringRedisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }

    /**
     * String 类型 get
     * */
    public String get(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * hash类型 hest
     */
    public  void hset(String key, String filed ,String value) {
        stringRedisTemplate.opsForHash().put(key, filed, value);
    }

    public List<String> hvals(String key) {
        return stringRedisTemplate.opsForHash().values(key)
                .stream().map(Object::toString).toList();
    }

    /**
     * hdel 删除
     */
    public void hdel(String key, String... fileds) {
        stringRedisTemplate.opsForHash().delete(key, fileds);
    }
}

